package com.ewanghuiju.app.widget.popupwindow;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.dp.RealmHelper;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 权益转化弹框
 * @time 2020/2/19
 */
public class VipTransformPopup extends BasePopupWindow {

    @BindView(R.id.et_water_num)
    EditText etWaterNum;
    //e网账号
    @BindView(R.id.tv_ew_no)
    TextView tvEwNo;
    //开团额度
    @BindView(R.id.tv_kted)
    TextView tvKted;
    private PopupItemClickCallback mPopupItemClickCallback;

    private CustomTipPopup mCustomTipPopup;

    public VipTransformPopup(Context context) {
        super(context);
        setWidth(getScreenWidth());
        ButterKnife.bind(this, getContentView());
        RealmHelper realmHelper = App.getAppComponent().realmHelper();
        UserInfoResponBean userInfoResponBean = realmHelper.getUserInfoResponBean();
        if (userInfoResponBean != null) {
            tvEwNo.setText(userInfoResponBean.getMobile());
        }
        etWaterNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 1) {
                    if (mPopupItemClickCallback != null) {
                        mPopupItemClickCallback.onTextChangedCallback(s.toString());
                    }
                }
            }
        });
        setAutoShowInputMethod(etWaterNum, true);
    }

    public void setPopupItemClickCallback(PopupItemClickCallback popupItemClickCallback) {
        this.mPopupItemClickCallback = popupItemClickCallback;
    }

    /**
     * 更新开团额度
     */
    public void setTvKted(String text) {
        if (tvKted != null) {
            tvKted.setText(TextUtils.isEmpty(text) ? "" : text);
        }
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_vip_transform);
    }

    @OnClick({R.id.popup_close, R.id.popup_sure, R.id.iv_yiwen})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.popup_close:
                dismiss();
                break;
            case R.id.popup_sure:
                //确定按钮
                if (mPopupItemClickCallback != null) {
                    mPopupItemClickCallback.onSureCallback(etWaterNum.getText().toString());
                }
                break;
            case R.id.iv_yiwen:
                //提示
                if (mCustomTipPopup != null) {
                    mCustomTipPopup.showPopupWindow();
                    return;
                }
                mCustomTipPopup = new CustomTipPopup(getContext(), getContext().getString(R.string.popup_tip_transform_title),
                        getContext().getString(R.string.popup_tip_transform_content), null);
                mCustomTipPopup.setPopupGravity(Gravity.CENTER);
                mCustomTipPopup.showPopupWindow();
                break;
            default:
                break;
        }
    }

//    @Override
//    public void onDismiss() {
//        super.onDismiss();
//        AutoSoftUtils.hideInput(etWaterNum);
//    }
}
